<template>
  <div>
    <!-- width使用百分比, max-width使用具体的xxx px , 用以适配小屏幕的PC端 -->
    <el-dialog
      :title="title"
      v-model="visable"
      width="45%"
      :close-on-click-modal="false"
      :draggable="true"
      style="max-width: 480px;"
    >
      <div class="bs-dialog">
        <el-form
          ref="addRef"
          :model="dialogFormData"
          :rules="dialogFormRules"
          label-width="auto"
          style="width: 80%;padding-left: 11%;text-align: left;"
        >
        <el-form-item label="手机号:" prop="studentID">
          <el-select
            filterable
            v-model="dialogFormData.studentID"
            placeholder="请选择手机号" 
            
            clearable
          >
          <el-option
              v-for="item in studentList"
              :key="item.id"
              :label="item.name+item.phone+item.account"
              :value="item.id"
            >
                <span>{{ item.name }}-{{ item.phone }}</span>
                <span style="float: right;color: #c7c7c7;">{{ item.account }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="权益类型: ">
          <el-select
            v-model="dialogFormData.productType"
            placeholder="请选择权益类型" 
            
            clearable
          >
            <el-option
              v-for="item in dictionaryTypeList"
              :key="item.dicValue"
              :label="item.dicName"
              :value="item.dicValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="购买份数:" prop="buyCopies">
          <el-input-number v-model="dialogFormData.buyCopies" :precision="2" placeholder="请输入购买份数额"  clearable/>
        </el-form-item>
        <el-form-item label="消费金额:" prop="monetary">
          <el-input-number v-model="dialogFormData.monetary" :precision="2" placeholder="请输入消费金额"  clearable/>
        </el-form-item>
        <el-form-item label="VIP服务:" prop="vipServer"  style="text-align: left;">
          <el-radio-group v-model="dialogFormData.vipServer">
            <el-radio :value="1">是</el-radio>
            <el-radio :value="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer" style="text-align: right;margin-right: 7%;">
          <el-button  size="small" @click="visable = false"> 取消 </el-button>
          <el-button  size="small" type="primary" @click="save"> 保存 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
  import { updateItem, addItem, getItemByID} from '@/api/oss/partnerShare.js'
import { selectDictionaryByTypeCode } from '@/api/system/dictionary.js'
import { getAll } from '@/api/oss/student.js'
// (修改和新增共用一个弹窗时,this.$refs['xxx'].resetFields()函数失效, 因此使用这种方式重置表单数据)
let orgDialogFormData = {
        id: undefined,
        studentID:"",
        productType:"",
        buyCopies:"",
        monetary:"",
        vipServer:"",
      }
export default {
  data() {
    return {
      title:"",
      visable: false,
      dialogFormData: { ...orgDialogFormData},
      dialogFormRules: {
        studentID: [
          { required: true, message: '请选择手机号', trigger: ['blur', 'change'] },
        ]
      },
      // 购课类型字典列表
      dictionaryTypeList:[],
      // 学员列表
      studentList:[],
    }
  },
  methods: {
    openModal(id) {
      console.log("openModal=",id);
      this.visable = true
      this.$nextTick(() => {
        this.dialogFormData = {...orgDialogFormData}
        selectDictionaryByTypeCode({"dicTypeCode":"PartnershipInterest"}).then(res=>{
          if (res.code == 1) {
            this.dictionaryTypeList = res.data
          }
        })
        if (this.isNotBlank(id)) {
          this.title = '修改购课信息'
          // 获取修改的信息
          getItemByID({ 'id': id }).then(res => {
            console.log("修改返回值=",res);
            if (res.code === 1) {
              this.dialogFormData = {...res.data}
            }
          })
        } else {
          this.title = '新增购课信息'
        }
        // 获取所有学员信息
        getAll({}).then(res=>{
          if (res.code == 1) {
            this.studentList = res.data
          }
        })
      })
    },
    
    // 保存
    save() {
      this.$refs['addRef'].validate((valid) => {
        if (valid) {
          if (this.isBlank(this.dialogFormData.id)) {
            addItem(this.dialogFormData).then(res => {
              if (res.code == 1) {
                this.$message.success('操作成功')
                this.visable = false
                // 刷新父组件中的表格数据
                this.$emit('flushTable', {})
              } else {
                this.$message.error(res.msg)
              }
            }).catch((error) => {
            })
          } else {
            updateItem(this.dialogFormData).then(res => {
              if (res.code == 1) {
                this.$message.success('操作成功')
                this.visable = false
                // 刷新父组件中的表格数据
                this.$emit('flushTable', {})
              } else {
                this.$message.error(res.msg)
              }
            }).catch((error) => {
            })
          }
          

        }
      })
    },

  }
}
</script>
<style scoped>
  :deep(.el-form-item__content) {
    display: inline-block;
    text-align: left;
    padding-right: 10%;
  }
  /deep/.el-drawer__title {
    text-align: left;
  }
  :deep(.el-input),
  :deep(.el-input-number--small) {
    width: 100%;
  }
</style>
